package com.stoneframe.controller.manageSystem;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stoneframe.controller.base.BaseGenericController;
import com.stoneframe.service.BaseService;
import com.stoneframe.service.manageSystem.SysAreaService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ToolHelper;

@Controller
@RequestMapping(value="sysArea")
public class SysAreaController extends BaseGenericController {
	
	@Resource(name="sysAreaService")
	private SysAreaService sysAreaService;

	@Override
	public BaseService getService() {
		// TODO Auto-generated method stub
		return sysAreaService;
	}
	
	/**
	 * 树JONS
	 */
	@ResponseBody
	@RequestMapping(value="treeJson",produces= "text/plain;charset=UTF-8")
	public Object treeJson(){
		logBefore(logger, className+"treeJson(树)");
		
		PageData pd=this.getPageData();
		List<PageData> list=null;
		StringBuilder sb = new StringBuilder();
		try{
			String parentId =pd.getString("value");
			pd.put("parentId", parentId);
			if(ToolHelper.isEmpty(parentId)){
				pd.put("parentId", "0");
			}
			list=sysAreaService.listAll(pd);
			sb.append("[");
			for(PageData item:list){
				PageData subItem=new PageData();
				subItem.put("parentId", item.getString("areaId"));
				Boolean hasChildren =sysAreaService.findCount(subItem)>0?true:false;
				sb.append("{");
                sb.append("\"id\":\"" + item.getString("areaId") + "\",");
                sb.append("\"text\":\"" +item.getString("areaName") + "\",");
                sb.append("\"value\":\"" + item.getString("areaId") + "\",");
                sb.append("\"isexpand\":false,");
                sb.append("\"complete\":false,");
                sb.append("\"hasChildren\":" + hasChildren.toString().toLowerCase() + "");
                sb.append("},");
			}
			sb = sb.delete(sb.length() - 1, sb.length());
			sb.append("]");
			
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}

		logAfter(logger);
		return AppHelper.returnObject(pd,sb.toString());
	}

}
